/************************************************************************
 * 描述 ：数据库表MES_AGE_DEV_BEFORE对应的Service，代码生成。
 * 作者 ：dev78
 * 日期 ：2021-01-21 15:54:45
 *
 ************************************************************************/

package com.pangus.ims.mes.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.pangus.PangusConstants;
import com.pangus.common.dto.EditDataResult;
import com.pangus.common.dto.JqGridRequest;
import com.pangus.common.dto.JqGridResponse;
import com.pangus.common.dto.JsonRequest;
import com.pangus.common.excel.callback.ExcelCallbackService;
import com.pangus.common.sys.dto.domain.LogDto;
import com.pangus.common.sys.service.CompanyService;
import com.pangus.common.sys.service.DataRolePointService;
import com.pangus.common.sys.service.ParameterService;
import com.pangus.common.sys.utils.CheckUtils;
import com.pangus.common.sys.utils.CommonUtils;
import com.pangus.common.sys.utils.CurrentDateContextHolder;
import com.pangus.common.sys.utils.MsgUtils;
import com.pangus.common.sys.utils.ParamUtils;
import com.pangus.common.sys.utils.PartnerCodeHolder;
import com.pangus.common.sys.utils.SqlQueryUtils;
import com.pangus.fa.i18n.FaMessageSource;
import com.pangus.fa.mybatis.mapper.entity.Condition;
import com.pangus.fa.mybatis.mapper.entity.Condition.Criteria;
import com.pangus.fa.mybatis.mapper.entity.Condition.Criterion;
import com.pangus.fa.service.BaseService;
import com.pangus.fa.util.FastIdUtils;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeBluetoothAllDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeCanDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeCpxDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeEsimAllDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeFcarDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeGpsDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeNandFlashDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeTBCommonInfoDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeTBInfoDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeUpsDto;
import com.pangus.ims.mes.dto.domain.AgeDevBeforeWifiDto;
import com.pangus.ims.mes.dto.domain.MesAgeDevBeforeDto;
import com.pangus.ims.mes.form.MesAgeDevBeforeForm;

@Service
public class MesAgeDevBeforeService extends BaseService<MesAgeDevBeforeDto> implements ExcelCallbackService<MesAgeDevBeforeForm>{

	@Autowired
	private FaMessageSource messageSource;
	
	@Autowired
    private ParameterService parameterService;
	
	@Autowired
    private DataRolePointService dataRolePointService;
	
	@Autowired
    private CompanyService companyService;
	
	@Autowired
	private SqlQueryUtils sqlQueryUtils;
	
	@Autowired
    private FastIdUtils fastIdUtils;
	
	/**
	 * 查询产品老化前关键测试信息列表
	 */
	public JqGridResponse<MesAgeDevBeforeForm> query(JqGridRequest<MesAgeDevBeforeForm> request,ParamUtils paramUtils) {
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		String pointData = dataRolePointService.getSql(paramUtils.getUserCode(), "MesAgeDevBefore.Query");
		String sql = getQuerySql();
		sql += " where 1=1 ";
		sql = CommonUtils.addCondition(sql, "m.", pointData, partnerCode,paramUtils);
		JqGridResponse<MesAgeDevBeforeForm> response = sqlQueryUtils.queryByCondition(request, sql, MesAgeDevBeforeForm.class);
		if(response.getData()!=null) {
			for (MesAgeDevBeforeForm form  : response.getData()) {
				if(form.getEsim()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  esim = JSONObject.parseObject(form.getEsim(), AgeDevBeforeTBCommonInfoDto.class);
						form.setEsimModel(esim.getModel());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getTerminal()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  terminal = JSONObject.parseObject(form.getTerminal(), AgeDevBeforeTBCommonInfoDto.class);
						form.setTerminalModel(terminal.getModel());
						form.setTerminalType(terminal.getType());
						form.setTerminalManufacturerId(terminal.getManufacturer().getId());
						form.setTerminalManufacturerProvinceId(terminal.getManufacturer().getProvinceId());
						form.setTerminalVersionHardware(terminal.getVersion().getHardware());
						form.setTerminalVersionFirmware(terminal.getVersion().getFirmware());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getSe()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  se = JSONObject.parseObject(form.getSe(), AgeDevBeforeTBCommonInfoDto.class);
						form.setSeId(se.getId());
						form.setSeManufacturerId(se.getManufacturer().getId());
						form.setSeManufacturerName(se.getManufacturer().getName());
						form.setSeModel(se.getModel());
					} catch (Exception e) {
						e.printStackTrace();
					}
						
				}
				
				if(form.getGnss()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  gnss = JSONObject.parseObject(form.getGnss(), AgeDevBeforeTBCommonInfoDto.class);
						form.setGnssModel(gnss.getModel());
						form.setGnssManufacturerName(gnss.getManufacturer().getName());
						form.setGnssAntennaModel(gnss.getAntenna().getModel());
						form.setGnssAntennaManufacturerName(gnss.getAntenna().getManufacturer().getName());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getComm()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  comm = JSONObject.parseObject(form.getComm(), AgeDevBeforeTBCommonInfoDto.class);
						form.setCommModel(comm.getModel());
						form.setCommManufacturerName(comm.getManufacturer().getName());
						form.setCommAntennaModel(comm.getAntenna().getModel());
						form.setCommAntennaManufacturerName(comm.getAntenna().getManufacturer().getName());
					
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getGsensor()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  gsensor = JSONObject.parseObject(form.getGsensor(), AgeDevBeforeTBCommonInfoDto.class);
						form.setGsensorModel(gsensor.getModel());
						form.setGsensorManufacturerName(gsensor.getManufacturer().getName());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getBluetooth()!=null) {
					try {
						JSONObject bluetooth = JSONObject.parseObject(form.getBluetooth());
//						AgeDevBeforeTBCommonInfoDto  bluetooth = JSONObject.parseObject(form.getBluetooth(), AgeDevBeforeTBCommonInfoDto.class);
						form.setBluetoothModel(bluetooth.getString("MODEL"));
						form.setBluetoothMac(bluetooth.getString("MAC"));
						form.setBluetoothVersion(bluetooth.getString("VERSION"));
						if(null!= bluetooth.getString("MANUFACTURER")) {
							JSONObject manufacturer = JSONObject.parseObject(bluetooth.getString("MANUFACTURER"));
							form.setBluetoothManufacturerName(manufacturer.getString("NAME"));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getStandbyBettery()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  standbyBettery = JSONObject.parseObject(form.getStandbyBettery(), AgeDevBeforeTBCommonInfoDto.class);
						form.setStandbyBetteryModel(standbyBettery.getModel());
						form.setStandbyBetteryType(standbyBettery.getType());
						form.setStandbyBetteryManufacturerName(standbyBettery.getManufacturer().getName());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getEtcObu()!=null) {
					try {
						AgeDevBeforeTBCommonInfoDto  etcObu = JSONObject.parseObject(form.getEtcObu(), AgeDevBeforeTBCommonInfoDto.class);
						form.setEtcObuModel(etcObu.getModel());
						form.setEtcObuId(etcObu.getId());
						form.setEtcObucardnum(etcObu.getCardnum());
						form.setEtcObuManufacturerId(etcObu.getManufacturer().getId());
						form.setEtcObuManufacturerName(etcObu.getManufacturer().getName());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getEsimAll()!=null) {
					try {
						AgeDevBeforeEsimAllDto  esim = JSONObject.parseObject(form.getEsimAll(), AgeDevBeforeEsimAllDto.class);
						form.setTerEsim(esim.getType());
						form.setTerIccid(esim.getIccid());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				
				if(form.getGps()!=null) {
					try {
						AgeDevBeforeGpsDto  gps = JSONObject.parseObject(form.getGps(), AgeDevBeforeGpsDto.class);
						form.setTerGpsSignal(gps.getSignal());
						form.setTerGpsSatellliteNumber(gps.getSatelllite_number());
						form.setTerSnr(gps.getSnr()==null?"":String.join(",", gps.getSnr()));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getUps()!=null) {
					try {
						AgeDevBeforeUpsDto  ups = JSONObject.parseObject(form.getUps(), AgeDevBeforeUpsDto.class);
						form.setTerUpsVersion(ups.getVersion());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getCan()!=null) {
					try {
						AgeDevBeforeCanDto  can = JSONObject.parseObject(form.getCan(), AgeDevBeforeCanDto.class);
						form.setTerUpsVersion(can.getVersion());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getCpx()!=null) {
					try {
						AgeDevBeforeCpxDto cpx = JSONObject.parseObject(form.getCpx(), AgeDevBeforeCpxDto.class);
						form.setTerCpxVersion(cpx.getVersion());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				if(form.getNandflash()!=null) {
					try {
						AgeDevBeforeNandFlashDto  nandflash = JSONObject.parseObject(form.getNandflash(), AgeDevBeforeNandFlashDto.class);
						form.setNandflashExpectSize(nandflash.getExpectSize());
						form.setNandflashRealSize(nandflash.getRealSize());
						form.setNandflashType(nandflash.getType());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(form.getWifi()!=null) {
					try {
						AgeDevBeforeWifiDto  wifi = JSONObject.parseObject(form.getWifi(), AgeDevBeforeWifiDto.class);
						form.setWifiMac(wifi.getMac());
						form.setWifiSignal(wifi.getSignal());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(form.getFcar()!=null) {
					try {
						AgeDevBeforeFcarDto fcar = JSONObject.parseObject(form.getFcar(), AgeDevBeforeFcarDto.class);
						form.setUid(fcar.getUid());
						form.setAuth(fcar.getAuth());
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(form.getBluetoothAll()!=null) {
					try {
						AgeDevBeforeBluetoothAllDto  bluetoothAll = JSONObject.parseObject(form.getBluetoothAll(), AgeDevBeforeBluetoothAllDto.class);
						form.setBluetoothAllVersion(bluetoothAll.getVersion());
						form.setBluetoothAllSignal(bluetoothAll.getSignal());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return response;
	}
	
	/**
	 * 查询单个产品老化前关键测试信息
	 * @param MesAgeDevBeforeDto 查询对象
	 * @param paramUtils 参数对象
	 * @return EditDataResult<MesAgeDevBeforeForm>
	 */
	public EditDataResult<MesAgeDevBeforeForm> getById(MesAgeDevBeforeDto mesAgeDevBeforeDto, ParamUtils paramUtils) {
        // 声明对象
		Locale locale = paramUtils.getLocale();
		LogDto logDto = paramUtils.getLogDto();
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		String pointData = dataRolePointService.getSql(paramUtils.getUserCode(), "MesAgeDevBefore.Query");
		// 必填
		checkUtils.checkIsEmpty("mesAgeDevBeforeId", mesAgeDevBeforeDto.getMesAgeDevBeforeId(), locale);
		// 校验数据是否存在
		String sql = getQuerySql();
		sql += " where m.MES_AGE_DEV_BEFORE_ID=? "; 
		sql = CommonUtils.addCondition(sql, "m.", pointData, partnerCode, paramUtils);
		MesAgeDevBeforeForm mesAgeDevBeforeForm = sqlQueryUtils.get(sql, new Object[] { mesAgeDevBeforeDto.getMesAgeDevBeforeId() },MesAgeDevBeforeForm.class);
		
	
		
		AgeDevBeforeTBInfoDto tb1 = new AgeDevBeforeTBInfoDto();
		
		if(mesAgeDevBeforeForm.getEsimAll()!=null) {
			try {
				AgeDevBeforeEsimAllDto  esim = JSONObject.parseObject(mesAgeDevBeforeForm.getEsimAll(), AgeDevBeforeEsimAllDto.class);
				//tb1.setEsim(esim);
				tb1.setEsimAll(esim);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getEsim()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  esim = JSONObject.parseObject(mesAgeDevBeforeForm.getEsim(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setEsim(esim);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getTerminal()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  terminal = JSONObject.parseObject(mesAgeDevBeforeForm.getTerminal(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setTerminal(terminal);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getSe()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  se = JSONObject.parseObject(mesAgeDevBeforeForm.getSe(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setSe(se);
			} catch (Exception e) {
				e.printStackTrace();
			}
				
		}
		
		if(mesAgeDevBeforeForm.getGnss()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  gnss = JSONObject.parseObject(mesAgeDevBeforeForm.getGnss(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setGnss(gnss);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getComm()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  comm = JSONObject.parseObject(mesAgeDevBeforeForm.getComm(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setComm(comm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getGsensor()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  gsensor = JSONObject.parseObject(mesAgeDevBeforeForm.getGsensor(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setGsensor(gsensor);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getBluetooth()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  bluetooth = JSONObject.parseObject(mesAgeDevBeforeForm.getBluetooth(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setBluetooth(bluetooth);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		if(mesAgeDevBeforeForm.getStandbyBettery()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  standbyBettery = JSONObject.parseObject(mesAgeDevBeforeForm.getStandbyBettery(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setStandbyBettery(standbyBettery);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(mesAgeDevBeforeForm.getEtcObu()!=null) {
			try {
				AgeDevBeforeTBCommonInfoDto  etcObu = JSONObject.parseObject(mesAgeDevBeforeForm.getEtcObu(), AgeDevBeforeTBCommonInfoDto.class);
				tb1.setEtcObu(etcObu);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		mesAgeDevBeforeForm.setTb1(tb1);
		//检查切面权限 
		checkUtils.checkHasAuthPointData(mesAgeDevBeforeForm.getOrgId(), dataRolePointService.getList(logDto.getUserCode(), "MesAgeDevBefore.Query"), locale);     
		// 返回
        EditDataResult<MesAgeDevBeforeForm> result = new EditDataResult<MesAgeDevBeforeForm>();
        result.setCount(PangusConstants.RESULT_COUNT_ONE);
        result.setResultCode(PangusConstants.RESTULT_OK);
        result.setData(mesAgeDevBeforeForm);
        return result;
    }
	
	/**
	 * 新增产品老化前关键测试信息
	 * @param dtoRequest 新增对象
	 * @param paramUtils 参数对象 
	 * @return EditDataResult<MesAgeDevBeforeDto>
	 */
	public EditDataResult<MesAgeDevBeforeDto> add(JsonRequest<MesAgeDevBeforeDto> dtoRequest,ParamUtils paramUtils) {
		MesAgeDevBeforeDto mesAgeDevBeforeDto=dtoRequest.getForm();
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);		
         
        //通用校验
        paramUtils.setCheckUtils(checkUtils);
        commonValidate(mesAgeDevBeforeDto,paramUtils,"Add");
        //新增
        mesAgeDevBeforeDto.setMesAgeDevBeforeId(fastIdUtils.nextId());
        mesAgeDevBeforeDto.setCreatedBy(paramUtils.getUserId());
        mesAgeDevBeforeDto.setUpdatedBy(paramUtils.getUserId());
        mesAgeDevBeforeDto.setCreatedDate(CurrentDateContextHolder.getCurrentDate());
        mesAgeDevBeforeDto.setUpdatedDate(CurrentDateContextHolder.getCurrentDate());
        mesAgeDevBeforeDto.setPartnerCode(PartnerCodeHolder.getPartnerCode());
		int count = this.insertNotNull(mesAgeDevBeforeDto);
		//校验返回值
		checkUtils.checkInsertDataIsOk(count, 1, (JSONObject)JSONObject.toJSON(mesAgeDevBeforeDto), MesAgeDevBeforeDto.class);
		//返回
		EditDataResult<MesAgeDevBeforeDto> result = new EditDataResult<MesAgeDevBeforeDto>();
		result.setData(mesAgeDevBeforeDto);
		result.setCount(count);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}
	
	
	/**
	 * 新增产品老化前关键测试信息
	 * @param dtoRequest 新增对象
	 * @param paramUtils 参数对象 
	 * @return EditDataResult<MesAgeDevBeforeDto>
	 */
	public EditDataResult<MesAgeDevBeforeDto> addAgeDevBefore(MesAgeDevBeforeDto mesAgeDevBeforeDto,ParamUtils paramUtils) {
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
         
        //新增
        mesAgeDevBeforeDto.setMesAgeDevBeforeId(fastIdUtils.nextId());
        mesAgeDevBeforeDto.setCreatedBy(paramUtils.getUserId());
        mesAgeDevBeforeDto.setUpdatedBy(paramUtils.getUserId());
        mesAgeDevBeforeDto.setCreatedDate(new Date());
        mesAgeDevBeforeDto.setUpdatedDate(new Date());
        mesAgeDevBeforeDto.setPartnerCode(PartnerCodeHolder.getPartnerCode());
		int count = this.insertNotNull(mesAgeDevBeforeDto);
		//返回
		EditDataResult<MesAgeDevBeforeDto> result = new EditDataResult<MesAgeDevBeforeDto>();
		result.setData(mesAgeDevBeforeDto);
		result.setCount(count);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}
	
	
	
	/**
	 * 修改产品老化前关键测试信息
	 * @param dtoRequest 修改对象
	 * @param paramUtils 参数对象 
	 * @return EditDataResult<MesAgeDevBeforeDto>
	 */
	public EditDataResult<MesAgeDevBeforeDto> edit(JsonRequest<MesAgeDevBeforeDto> dtoRequest,ParamUtils paramUtils) {
		MesAgeDevBeforeDto mesAgeDevBeforeDto=dtoRequest.getForm();
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		//通用校验
		paramUtils.setCheckUtils(checkUtils);
		commonValidate(mesAgeDevBeforeDto, paramUtils,"Edit");
        //验证数据是否不存在
		MesAgeDevBeforeDto mesAgeDevBeforeDb = this.selectByPrimaryKey(mesAgeDevBeforeDto); 
		checkUtils.checkIsEmpty("mesAgeDevBeforeId", mesAgeDevBeforeDb.getMesAgeDevBeforeId(), locale);
		// 验证不能修改的数据是否被修改：编码不可修改
		checkUtils.checkIsChanged(new String[] { "partnerCode", "orgId"},(JSONObject) JSONObject.toJSON(mesAgeDevBeforeDto), (JSONObject) JSONObject.toJSON(mesAgeDevBeforeDb), locale);
		// 验证数据是否已被修改
		checkUtils.checkIsChanged(mesAgeDevBeforeDto.getUpdatedDate(), mesAgeDevBeforeDb.getUpdatedDate(), locale);	
		//修改		
        Condition condition = new Condition(MesAgeDevBeforeDto.class);
        Criteria criteria=setBasicCriteria(condition,mesAgeDevBeforeDto);
        MesAgeDevBeforeDto dto=new MesAgeDevBeforeDto();
        //修改字段
        BeanUtils.copyProperties(mesAgeDevBeforeDto, dto, new String[]{"mesAgeDevBeforeId","partnerCode","createdBy","createdDate","updatedBy","updatedDate"});
        dto.setCreatedBy(mesAgeDevBeforeDb.getCreatedBy());
        dto.setCreatedDate(mesAgeDevBeforeDb.getCreatedDate());
        dto.setUpdatedBy(paramUtils.getUserId());
        dto.setUpdatedDate(CurrentDateContextHolder.getCurrentDate());
        
		int count = this.updateByCondition(dto, condition);
		checkUtils.checkUpdateDataIsOk(count, 1, getMapByCriteria(criteria), MesAgeDevBeforeDto.class);
		//封装返回数据
		EditDataResult<MesAgeDevBeforeDto> result = new EditDataResult<MesAgeDevBeforeDto>();
		result.setCount(count);
		result.setData(mesAgeDevBeforeDto);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}
	
	/**
	 * 删除产品老化前关键测试信息
	 * @param dtoList 删除对象
	 * @param paramUtils 参数对象
	 * @return EditDataResult<MesAgeDevBeforeDto>
	 */
	public EditDataResult<MesAgeDevBeforeDto> delete(List<MesAgeDevBeforeDto> dtoList,ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
        CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
        // 校验空值
        checkUtils.checkIsEmpty("mesAgeDevBeforeId", dtoList, locale);
        for (MesAgeDevBeforeDto dto : dtoList) {
            // 非空验证
            checkUtils.checkIsEmpty("mesAgeDevBeforeId", dto.getMesAgeDevBeforeId(), locale);
           	checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
            // 验证数据是否不存在
            MesAgeDevBeforeDto mesAgeDevBeforeDb = this.selectByPrimaryKey(dto);
             
			// 校验是否有此数据权限 
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "MesAgeDevBefore.Delete"); 
			companyService.checkCompanyData(mesAgeDevBeforeDb.getOrgId(), pointData, checkUtils, locale);
 			Condition condition = new Condition(MesAgeDevBeforeDto.class);
        	Criteria criteria=setBasicCriteria(condition,dto);
        	int deleteCount = this.deleteByCondition(condition);
            checkUtils.checkDeleteDataIsOk(deleteCount, 1, getMapByCriteria(criteria), MesAgeDevBeforeDto.class);
            count = count + deleteCount;
        }
        //返回
        EditDataResult<MesAgeDevBeforeDto> result = new EditDataResult<MesAgeDevBeforeDto>();
        result.setCount(count);
        result.setResultCode(PangusConstants.RESTULT_OK);
        result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
        return result;
	}
	/**
	 * 检查工厂权限  若没有工厂字段，此方法无效
	 * @param dtoList 检查对象
	 * @param dataPointId 切入点Id
	 * @param paramUtils 参数对象
	 * @return EditDataResult<List<String>>
	 */
	public EditDataResult<List<String>> checkPointData(List<MesAgeDevBeforeDto> dtoList,String dataPointId,ParamUtils paramUtils) {
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		EditDataResult<List<String>> result = new EditDataResult<List<String>>();
		CheckUtils checkUtil = new CheckUtils(messageSource, parameterService, logDto);
        //验证非空
        checkUtil.checkIsEmpty(PangusConstants.DATA_POINT_ID, dataPointId, locale);
        //验证是否有工厂权限
        List<String> pointData = dataRolePointService.getList(paramUtils.getUserCode(), dataPointId.toString());
        for (MesAgeDevBeforeDto dto : dtoList) {
        	MesAgeDevBeforeDto dbData = this.selectByPrimaryKey(dto);
            checkUtil.checkIsNotExist("mesAgeDevBeforeId", dbData, locale); 
		String companyCode = dbData.getOrgId(); 
		// 校验是否有此数据权限 
		checkUtil.checkHasAuthPointData(companyCode, pointData, locale);
        }
        //返回
        result.setCount(1);
        result.setData(pointData);
        return result;
    }
    /**
	 * 通用检验
	 * @param MesAgeDevBeforeDto 验证对象 
	 * @param paramUtils 参数对象
	 */
    public void commonValidate(MesAgeDevBeforeDto mesAgeDevBeforeDto,ParamUtils paramUtils,String type){
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = paramUtils.getCheckUtils();
		//非空验证
		checkUtils.checkIsEmpty("mesAgeDevBeforeId", mesAgeDevBeforeDto, locale);
		if(type=="Edit"){
        	checkUtils.checkIsEmpty("mesAgeDevBeforeId", mesAgeDevBeforeDto.getMesAgeDevBeforeId(), locale);
			checkUtils.checkIsEmpty("updatedDate", mesAgeDevBeforeDto.getUpdatedDate(), locale);
		}
        
		checkUtils.checkIsEmpty("orgId", mesAgeDevBeforeDto.getOrgId(), locale);    
		//验证关联数据值，工厂编码存在性及是否有权限 
		List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "MesAgeDevBefore."+type); 
		companyService.checkCompanyData(mesAgeDevBeforeDto.getOrgId(), pointData, checkUtils, locale);
		//验证唯一索引
        Condition condition = new Condition(MesAgeDevBeforeDto.class);
        Criteria criteria = condition.createCriteria();
        criteria.andNotEqualTo("mesAgeDevBeforeId", mesAgeDevBeforeDto.getMesAgeDevBeforeId());
        criteria.andEqualTo("partnerCode", PartnerCodeHolder.getPartnerCode());  
		criteria.andEqualTo("orgId", mesAgeDevBeforeDto.getOrgId());
		List<MesAgeDevBeforeDto> mesAgeDevBeforeDtos = this.selectByCondition(condition);  
	}

	/**
	*查询本表sql
	*根据orgid是否关联组织表
	*/
	public String getQuerySql() {
		String sql ="SELECT m.mcu_version,m.gps,m.ups, "+
				"	m.test_reason,m.before_mobile,m.age_prod_type, "+
				"	m.valid_flag, "+
				"	m.mes_age_dev_before_id, "+
				"	m.org_id, "+
				"	m.partner_code, "+
				"	m.before_conn_name, "+
				"	m.before_type, "+
				"	m.before_sub_model, "+
				"	m.before_chip, "+
				"	m.before_imei1, "+
				"	m.before_imei2, "+
				"	m.before_result, "+
				"	m.before_count, "+
				"	m.before_barcode, "+
				"	m.before_version, "+
				"	m.before_test_time, "+
				"	m.before_mac, "+
				"	u.user_name as created_by, "+
				"	m.created_date, "+
				"	m.updated_by, "+
				"	m.updated_date, "+
				"	m.mes_age_dev_before_ex1, "+
				"	m.mes_age_dev_before_ex2, "+
				"	m.mes_age_dev_before_ex3, "+
				"	m.mes_age_dev_before_ex4, "+
				"	m.mes_age_dev_before_ex5, "+
				"	m.mes_age_dev_before_ex6, "+
				"	m.mes_age_dev_before_ex7, "+
				"	m.mes_age_dev_before_ex8, "+
				"	m.mes_age_dev_before_ex9 , "+
				"	c.message_key org_name, "+
				"	m.mo_code, "+
				"	m.erp_mo, "+
				"   m.mo_id, "+
				"	m.prod_code, "+
				"	m.prod_name, "+
				"	m.ESIM, "+
				"	m.TERMINAL, "+
				"	m.SE, "+
				"	m.GNSS, "+
				"	m.COMM, "+
				"	m.GSENSOR, "+
				"	m.BLUETOOTH, "+
				"	m.STANDBY_BETTERY, "+
				"	m.IS_ETC_SUPPORT, "+
				"	m.ETC_OBU, "+
				"   m.ESIM_ALL, "+
				"   m.FCAR "+
				"FROM "+
				"	MES_AGE_DEV_BEFORE m "+
				" LEFT JOIN IMS_USER u on u.user_id = m.created_by "+
				"LEFT JOIN ims_company c ON "+
				"	c.company_code = m.org_id  and c.partner_code={partnerCode} ";
		return sql;
	}
	/**
	 * 设置基本查询条件
	 * @param condition
	 * @param mesAgeDevBeforeDto
	 * @return
	 */
	private Criteria setBasicCriteria(Condition condition,MesAgeDevBeforeDto mesAgeDevBeforeDto){
		 Criteria criteria = condition.createCriteria();
		 criteria.andEqualTo("mesAgeDevBeforeId", mesAgeDevBeforeDto.getMesAgeDevBeforeId());
		 criteria.andEqualTo("partnerCode", PartnerCodeHolder.getPartnerCode());
		return criteria;
	}
	/**
	 * criteria转map
	 * @param criteria
	 * @return
	 */
	private Map<String,Object> getMapByCriteria(Criteria criteria){
		Map<String,Object> map=new HashMap<String,Object>();
		 List<Criterion> list=criteria.getCriteria();
		 for(Criterion criterion:list){
			 map.put(criterion.getCondition(),criterion.getValue());
		 }
		 return map;
	}
	@Override
	public void importConvert(int i, List<MesAgeDevBeforeForm> list, ParamUtils paramUtils) {
		for (MesAgeDevBeforeForm MesAgeDevBeforeForm : list) {
			commonValidate(MesAgeDevBeforeForm, paramUtils, "Add");
		}
	}

	@Override
	public void exportConvert(int j, List<MesAgeDevBeforeForm> list, ParamUtils paramUtils) {
	}
}